define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ship_order/index' + location.search,
                    add_url: 'ship_order/add',
                    edit_url: 'ship_order/edit',
                    del_url: 'ship_order/del',
                    multi_url: 'ship_order/multi',
                    table: 'ship_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'signagent.name', title: __('Sign_agent_id'), operate: 'LIKE'},
                        {field: 'send.name', title: __('Send_agent_id'), operate: 'LIKE'},
                        {field: 'send.role', title: __('发货代理等级'), searchList: Config.levelList, visible: false},
                        {field: 'send.role_text', title: __('发货代理等级'), operate: false},
                        {field: 'postman.name', title: __('Postman_id'), operate: 'LIKE'},
                        {field: 'shipping_name', title: __('Shipping_name'), operate: 'LIKE'},
                        {field: 'shipping_no', title: __('Shipping_no'), operate: 'LIKE'},
                        {field: 'destory_number', title: __('核销数量')},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false, formatter: Table.api.formatter.status, custom:{'已签收':'success','待签收':'warning'}},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'sign_time', title: __('Sign_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'send_code', title: __('Send_code'), operate: 'LIKE'},
                        {field: 'sign_code', title: __('Sign_code'), operate: 'LIKE'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                name: 'spec',
                                text: '查看商品',
                                title: '查看商品',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                url: function(row, column){
                                    return 'ship_item/index?order_id='+row.id;
                                },
                            },{
                                    name: 'sign',
                                    text: '签收订单',
                                    title: '签收订单',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'ship_order/sign'
                                }],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                if(row.status != 0) {
                                    $(table).data("operate-sign", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        sign: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});